/**
 * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * For the applicable distribution terms see the license.txt -file, included in
 * the distribution.
 */

#ifndef PRODUCTCATALOGMODEL_H
#define PRODUCTCATALOGMODEL_H

#include <QSortFilterProxyModel>
#include <QImage>

class DeclarativePurchaseListModel;

/*!
  \class ProductCatalogModel
  \brief Implements list model sorting functionality to
         DeclarativePurchaseListModel class.
*/
class ProductCatalogModel : public QSortFilterProxyModel
{
    Q_OBJECT
    Q_PROPERTY(bool isApplicationBusy READ isApplicationBusy NOTIFY applicationBusyChanged)

public:
    /*!
      Constructor
    */
    explicit ProductCatalogModel(QObject *parent = 0);

    /*!
      Interface for QML side.
      Buy product request for item with \a index.
     */
    Q_INVOKABLE void buy(int index);

    /*!
      Interface for QML side.
      Requests IAP client to restore all unlocked products.
     */
    Q_INVOKABLE void restoreProducts();

    /*!
      Interface for QML side.
      Requests IAP client for user info.
     */
    Q_INVOKABLE void getUserInfo();

    /*!
      Creates a product icon for item with \a index. Returns the created icon.
     */
    QImage createIcon(int index);

    /*!
      Property accessor.
    */
    bool isApplicationBusy() const;

    /*!
      Getter for product list model.
    */
    inline const DeclarativePurchaseListModel *dataModel() const
    {
        return m_iapModel;
    }

signals:
    /*!
      Signal emitted when product list model busy state has changed.
    */
    void applicationBusyChanged();

    /*!
      Signal emitted when product buy request has completed.
    */
    void appPurchaseCompleted(QVariant status);

public slots:
    /*!
      Slot for catching model updated signal from underlying model.
    */
    void modelUpdated();

    /*!
      Slot for catching busy state change signal from underlying model.
    */
    void modelBusyChanged();

    /*!
      Slot for catching buy complete signal from underlying model.
    */
    void purchaseCompleted(bool status);

private:
    /*!
      Base class virtual method implementation.
    */
    bool lessThan(const QModelIndex &left, const QModelIndex &right) const;

private: // Data
    DeclarativePurchaseListModel *m_iapModel; // product list model, owned
};

#endif // PRODUCTCATALOGMODEL_H
